package com.kvn.btrace._02probeMapExpand;

import java.util.Map;

import com.sun.btrace.BTraceUtils;
import com.sun.btrace.BTraceUtils.Strings;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.Self;

/**
 * @author wzy
 * @date 2017-9-22 10:23:36
 */
@BTrace
public class ExpandCapacityBtrace {
	
	@OnMethod(clazz = "java.util.HashMap", method = "resize", location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/"))
	public static void traceMapExpandCapacity(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod, @Self Object self,
			int newCapacity) {
		String point = Strings.strcat(Strings.strcat(probeClass, "."), probeMethod);// java/util/HashMap.resize
		Class clazz = BTraceUtils.classForName("java.util.HashMap");
		BTraceUtils.println(Strings.strcat(point, "======"));
		// get protected field
		Map.Entry[] table = (Map.Entry[]) BTraceUtils.get(BTraceUtils.field(clazz, "table", true), self);
		int threshold = BTraceUtils.getInt(BTraceUtils.field(clazz, "threshold", true), self);
		int size = BTraceUtils.getInt(BTraceUtils.field(clazz, "size", true), self);
		BTraceUtils.println(Strings.strcat("newCapacity:", BTraceUtils.str(newCapacity)));
		BTraceUtils.println(Strings.strcat("table.length:", BTraceUtils.str(table.length)));
		BTraceUtils.println(Strings.strcat("size:", BTraceUtils.str(size)));
		BTraceUtils.println(Strings.strcat("threshold:", BTraceUtils.str(threshold)));
		BTraceUtils.println(Strings.strcat(point, "------------"));
	}
	
}
